package com.travel.server.manager.impl.command.user;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.user.UpdateDepartmentRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.DepartmentDal;
import com.travel.server.manager.model.DepartmentInfo;
import com.travel.server.manager.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/20 14:26
 * @Desc:
 */
@Component
public class UpdateDepartmentCommand extends AbstractCommand<UpdateDepartmentRequest, EmptyResponse> {

    @Autowired
    private DepartmentDal departmentDal;

    @Override
    protected void checkInput(UpdateDepartmentRequest request) {

    }

    @Override
    public EmptyResponse handle(UpdateDepartmentRequest request) {
        UserInfo userInfo = getUserInfo();
        Date now = new Date();

        DepartmentInfo departmentInfo;
        if(request.getId() == null){
            departmentInfo = new DepartmentInfo();
            departmentInfo.setCreateBy(userInfo.getName());
            departmentInfo.setCreateTime(now);
            departmentInfo.setState(CommonConstant.YES);
        }else {
            departmentInfo = departmentDal.selectById(request.getId());
        }
        departmentInfo.setUpdateTime(now);
        departmentInfo.setUpdateBy(userInfo.getName());
        departmentInfo.setDepartmentDescribe(request.getDescribe());
        departmentInfo.setDepartmentName(request.getName());
        departmentDal.insertOrUpdate(departmentInfo);
        return new EmptyResponse();
    }
}
